package com.starbaba.loanApiRecord.view;

import com.opensymphony.xwork2.ActionSupport;
import com.starbaba.core.utils.PropertyParamMapBuilder;
import com.starbaba.core.utils.Struts2Utils;
import com.starbaba.core.utils.json.JsonUtils;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.core.web.struts2.BaseAction;
import com.starbaba.loanApiRecord.model.LoanApiRecordModel;
import com.starbaba.loanApiRecord.service.LoanApiRecordService;
import com.starbaba.loanApiRecord.service.impl.LoanApiRecordServiceImpl;
import com.tuanzi.server.loan_base.util.PhoneNumNideUtil;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

public class LoanApiRecordAction extends BaseAction {
	
	private static final long serialVersionUID = 1L;
	
	private LoanApiRecordService service = new LoanApiRecordServiceImpl();
	private LoanApiRecordModel model;
	private InputStream input;
	private String fileName;

	
	
	/**
	 * 联合登录API记录列表
	 */
	public void getLoanApiRecordList() {

		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		parsePageRequest();

		PageResponse pageResponse = new PageResponse();
		int start = pageRequest.getIDisplayStart();
		int length = pageRequest.getIDisplayLength();

		List<LoanApiRecordModel> list = service.getApiRecordList(start, length, pageResponse, paramsMap);
		pageResponse.setAaData(list);
		pageResponse.setSEcho(pageRequest.getSEcho());
		try {
			sendJsonMessage(JsonUtils.toJson(pageResponse, null, true, null, false));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 导出数据
	 */
	public String exportDetailCSV() throws UnsupportedEncodingException{

		parsePageRequest();
		PageResponse pageResponse = new PageResponse();
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);

		pageRequest.setIDisplayStart(-1);
		pageRequest.setIDisplayLength(-1);
		pageRequest.setOrderby("createtime");
		pageRequest.setOrder("desc");

		int start = pageRequest.getIDisplayStart();
		int length = pageRequest.getIDisplayLength();

		List<LoanApiRecordModel> list = service.getApiRecordList(start, length, pageResponse, paramsMap);
		String value = service.exportLoanOrderCVS(list);
		input = new ByteArrayInputStream(value.getBytes("GBK"));
		fileName = System.currentTimeMillis() + ".csv";
		return ActionSupport.SUCCESS;
	}

	public InputStream getInput() {
		return input;
	}

	public void setInput(InputStream input) {
		this.input = input;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	
	
	
	
	
	
}
